using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using Toenda.TFL.Base;
using Toenda.Rails.System;
using Toenda.Rails.DataContainer;

public partial class Controls_Main_ctrl_editimage : System.Web.UI.UserControl {
	private AccountProvider ap = new AccountProvider();
	private ImageProvider ip = new ImageProvider();
	private StaticProvider sp = new StaticProvider();

	/// <summary>
	/// Page_Load
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void Page_Load(object sender, EventArgs e) {
		if(Session.Count > 0 && ap.CheckSessionExist(Session["Zappr"].ToString())) {
			if(Request.QueryString.Get("photo") != null) {
				ImageItem item = ip.GetImage(Convert.ToInt64(Request.QueryString.Get("photo")));
				Account tmp = ap.GetAccountBySession(Session["Zappr"].ToString());
				Account acc = ap.GetAccount(item.UserID);

				if(item.UserID == tmp.ID) {
					/*
					 * image
					 */
					imgEdit.ImageUrl = "../../Images/" + item.Image240;
					txtTitle.Text = item.Name;
					txtDesc.Text = item.Description;

					rblPrivacy.SelectedValue = item.Access.ToString();

					foreach(TagItem tag in item.Tags) {
						if(txtTags.Text.Trim() != "") {
							txtTags.Text += " ";
						}

						txtTags.Text += tag.Tag;
					}

					ArrayList arr = null;

					if(acc.Type != 0) {
						plcLicence.Visible = true;

						arr = sp.GetStaticItems("L001");

						ddlLicence.DataSource = arr;
						ddlLicence.DataValueField = "Value";
						ddlLicence.DataTextField = "Content";
						ddlLicence.DataBind();
						ddlLicence.Items.Insert(0, new ListItem(GetLocalResourceObject("NoLicence").ToString(), "--"));

						ddlLicence.SelectedValue = item.Licence;
					}

					if(tmp.Language == "de-DE") {
						arr = sp.GetStaticItems("IT02");
					}
					else {
						arr = sp.GetStaticItems("IT01");
					}

					this.ddlType.DataSource = arr;
					this.ddlType.DataValueField = "Value";
					this.ddlType.DataTextField = "Content";
					this.ddlType.DataBind();

					this.ddlType.SelectedValue = item.Type.ToString();
				}
				else {
					Response.Redirect("~/photo/" + Request.QueryString.Get("photo") + "/default.aspx");
				}
			}
			else {
				Response.Redirect("~/dashboard/default.aspx");
			}
		}
		else {
			plcEdit.Visible = false;
			Response.Redirect("~/login.aspx");
		}
	}

	/// <summary>
	/// btnSave_Click
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnSave_Click(object sender, EventArgs e) {
		ImageItem item = ip.GetImage(Convert.ToInt64(Request.QueryString.Get("photo")));
		Account acc = ap.GetAccount(item.UserID);

		item.Access = Convert.ToInt32(rblPrivacy.SelectedValue);
		item.Name = txtTitle.Text.Trim();
		item.Description = txtDesc.Text.Trim();

		if(txtTags.Text.Trim() != "") {
			string[] tags = txtTags.Text.Split(new char[] { ';', ',', ' ' });

			item.Tags.Clear();

			for(int i = 0; i < tags.Length; i++) {
				if(tags[i].Trim() != "") {
					TagItem tag = new TagItem();
					tag.ID = Guid.NewGuid().ToString();
					tag.Tag = tags[i];

					item.Tags.Add(tag);
				}
			}
		}

		if(acc.Type != 0) {
			item.Licence = ddlLicence.SelectedValue;
		}

		if(Convert.ToInt32(ddlType.SelectedValue) > 0) {
			item.Type = Convert.ToInt32(ddlType.SelectedValue);
		}
		else {
			item.Type = 1;
		}

		ip.SaveImage(item);

		//Response.Redirect("default.aspx?item=" + item.ImageID.ToString());
		Response.Redirect("~/photo/" + item.ImageID.ToString() + "/default.aspx");
	}

	/// <summary>
	/// btnCancel_Click
	/// </summary>
	/// <param name="sender"></param>
	/// <param name="e"></param>
	protected void btnCancel_Click(object sender, EventArgs e) {
		if(Request.QueryString.Get("photo") != null) {
			ImageItem item = ip.GetImage(Convert.ToInt64(Request.QueryString.Get("photo")));
			Account acc = ap.GetAccount(item.UserID);

			//Response.Redirect("default.aspx?id=images&user=" + acc.Username);
			Response.Redirect("~/images/" + acc.Username + "/default.aspx");
		}
		else {
			//Response.Redirect("default.aspx?id=dashboard");
			Response.Redirect("~/dashboard/default.aspx");
		}
	}
}
